package com.sysmanage.etl.controls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sysmanage.etl.model.ETL_CovertFactory;
import com.sysmanage.etl.model.ETL_Property;
import com.sysmanage.etl.service.ICovertFactoryService;
import com.sysmanage.etl.service.IPropertyService;
import com.sysmanage.etl.utils.ETLUtil;
import com.sysmanage.etl.vo.CovertFactoryVO;

@Scope("prototype")
@Controller(value = "covertFactoryTg")
public class CovertFactoryTg extends BaseETLTg {
	private static final long serialVersionUID = -4844218616940876320L;

	@Resource
	private ICovertFactoryService covertFactoryService;
	@Resource
	private IPropertyService propertyService;
	List<CovertFactoryVO> covertFactoryList = new ArrayList<CovertFactoryVO>();

	private String preValue;
	private String behindValue;
	private Integer propertyId;
	private Integer covertFactoryId;

	private Map<String, Object> result = new HashMap<String, Object>();

	public String index() {
		return "index";
	}

	public void update() {
		ETL_CovertFactory covertFactory = this.covertFactoryService.findById(covertFactoryId);
		covertFactory.setFactory_preValue(preValue);
		covertFactory.setFactory_behindValue(behindValue);
		boolean isSuccess = covertFactoryService.alter(covertFactory);

		if (isSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		this.outJsonPlainString(response, jsonObject.toString());
	}

	public void add() {
		ETL_CovertFactory covertFactory = new ETL_CovertFactory();
		ETL_Property property = this.propertyService.findById(propertyId);
		covertFactory.setShop_id(this.getShopId());
		covertFactory.setProperty(property);
		covertFactory.setFactory_preValue(preValue);
		covertFactory.setFactory_behindValue(behindValue);
		boolean isSuccess = covertFactoryService.save(covertFactory);
		if (isSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		this.outJsonPlainString(response, jsonObject.toString());
	}

	public void delete() {
		boolean isSuccess = this.covertFactoryService.deleteById(covertFactoryId);
		if (isSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		this.outJsonPlainString(response, jsonObject.toString());
	}

	public void list() {
		String hql = null;
		if(ETLUtil.isAdmin(this.getShopId())){
			hql = "from ETL_CovertFactory cf order by cf.shop_id";
			this.total = covertFactoryService.findCountBySql("from ETL_CovertFactory");
		}else{
			hql = "from ETL_CovertFactory cf  where shop_id = " + this.getShopId();
			this.total = covertFactoryService.findCountBySql("from ETL_CovertFactory where shop_id = " + this.getShopId() + " group by shop_id");
		}
		List<ETL_CovertFactory> list = covertFactoryService.findList(hql, page, rows);
		

		for (ETL_CovertFactory factory : list) {
			CovertFactoryVO vo = new CovertFactoryVO();
			vo.setBehindValue(factory.getFactory_behindValue());
			vo.setPreValue(factory.getFactory_preValue());
			vo.setId(factory.getFactory_id());
			vo.setDataSource(factory.getProperty().getTable().getDataSource().getDataSource_name());
			vo.setTable(factory.getProperty().getTable().getTable_name());
			if (factory.getProperty().getProperty_alias() != null) {
				vo.setProperty(factory.getProperty().getProperty_alias() + "(" + factory.getProperty().getProperty_name() + ")");
			} else {
				vo.setProperty(factory.getProperty().getProperty_name());
			}

			vo.setShopId(factory.getShop_id() + "");
			vo.setShopName(ETLUtil.convertID2Name(this.getShopId()));
			this.covertFactoryList.add(vo);
		}
		JSONArray jsonArray = JSONArray.fromObject(this.covertFactoryList);
		this.outJsonPlainString(response, jsonArray.toString());
	}

	public void setPreValue(String preValue) {
		this.preValue = preValue;
	}

	public void setBehindValue(String behindValue) {
		this.behindValue = behindValue;
	}

	public void setCovertFactoryId(Integer covertFactoryId) {
		this.covertFactoryId = covertFactoryId;
	}

	@JSON(name = "rows")
	public List<CovertFactoryVO> getCovertFactoryList() {
		return covertFactoryList;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setPropertyId(Integer propertyId) {
		this.propertyId = propertyId;
	}
	
	private Integer total = 0;

	public Integer getTotal() {
		return total;
	}
}
